<template>
  <div class="common-layout">
    <el-container>
      <el-header>
      </el-header>
      <el-container>
        <el-aside width="200px" class="sidebar">
          <div class="sidebar-header">
            <h3>生产商功能</h3>
          </div>
          
          <div class="sidebar-menu" v-if="authStore.isLoggedIn">
            <button 
              @click="activeTab = 'changePassword'" 
              :class="{ active: activeTab === 'changePassword' }" 
              class="menu-item"
            >修改密码</button>
            <button 
              @click="activeTab = 'goodsMadeInfo'" 
              :class="{ active: activeTab === 'goodsMadeInfo' }" 
              class="menu-item"
            >提交商品制造信息</button>
          </div>
          
          <div v-else class="sidebar-login-prompt">
            <button @click="goHome" class="back-home-tab">前往登录</button>
          </div>
          
          <!-- 侧边栏底部退出按钮 -->
          <div class="sidebar-footer" v-if="authStore.isLoggedIn">
            <button @click="handleLogout" class="logout-btn">退出登录</button>
          </div>
          
        </el-aside>
        
        <el-main class="main-content">
          <!-- 修改密码表单 -->
          <div v-if="activeTab === 'changePassword'" class="business-card">
            <h3>修改密码</h3>
            <form @submit.prevent="handleChangePassword" class="password-form">
              <div class="form-group">
                <label>账号:</label>
                <input v-model="changePasswordForm.accountId" placeholder="账号" required>
              </div>
              <div class="form-group">
                <label>旧密码:</label>
                <input v-model="changePasswordForm.oldPassword" type="password" placeholder="旧密码" required>
              </div>
              <div class="form-group">
                <label>新密码:</label>
                <input v-model="changePasswordForm.newPassword" type="password" placeholder="新密码" required>
              </div>
              <button type="submit" class="submit-btn">修改密码</button>
            </form>
          </div>
          
          <!-- 提交商品制造信息表单 -->
          <div v-if="activeTab === 'goodsMadeInfo'" class="business-card">
            <h3>提交商品制造信息</h3>
            <form @submit.prevent="handlePostGoodsMadeInfo" class="goods-form">
              <div class="form-group">
                <label>商品名称:</label>
                <input v-model="goodsForm.productName" placeholder="商品名称" required>
              </div>
              <div class="form-group">
                <label>制造商ID:</label>
                <input v-model="goodsForm.manufacturerId" placeholder="制造商ID" readonly>
              </div>
              <div class="form-group">
                <label>是否通过质检:</label>
                <select v-model="goodsForm.qualityInspection" required>
                  <option value="1">是</option>
                  <option value="0">否</option>
                </select>
              </div>
              <div class="form-actions">
                <button type="submit" class="submit-btn">提交</button>
                <button type="button" @click="resetGoodsForm" class="cancel-btn">重置</button>
              </div>
            </form>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { useAuthStore } from '../stores/auth'
import ApiService from '../services/api'

export default {
  name: 'ManufacturerView',
  setup() {
    const authStore = useAuthStore()
    return { authStore }
  },
  data() {
    return {
      activeTab: 'goodsMadeInfo',
      changePasswordForm: {
        accountId: '',
        oldPassword: '',
        newPassword: ''
      },
      goodsForm: {
          productName: '',
          manufacturerId: '',
          qualityInspection: '0' // 默认未通过质检
        },
      apiService: new ApiService('manufacturer')
    }
  },
  mounted() {
    this.authStore.initAuth()
    if (this.authStore.isLoggedIn) {
      this.activeTab = 'goodsMadeInfo'
      // 自动填充当前登录账号
      this.changePasswordForm.accountId = this.authStore.accountId
      this.goodsForm.manufacturerId = this.authStore.accountId
    }
  },
  methods: {
    async handleChangePassword() {
      try {
        const result = await this.apiService.changePassword(
          this.changePasswordForm.accountId,
          this.changePasswordForm.oldPassword,
          this.changePasswordForm.newPassword
        )
        alert(result.message)
      } catch (error) {
        alert('修改密码出错')
      }
    },
    
    async handlePostGoodsMadeInfo() {
      // try {
        // 创建提交数据的副本，确保qualityInspection格式正确
        const submitData = {
          ...this.goodsForm,
          // 将字符串值转换为数字
          qualityInspection: this.goodsForm.qualityInspection === '1' ? 1 : 0
        }
        const result = await this.apiService.postGoodsMadeInfo(submitData)
        alert(result.message)
        if (result.message === '商品信息提交成功') {
          this.resetGoodsForm()
        }
      // } catch (error) {
      //   alert('提交商品信息出错')
      // }
    },

    resetGoodsForm() {
      this.goodsForm = {
        productName: '',
        manufacturerId: this.authStore.accountId,
        qualityInspection: '0' // 默认未通过质检
      }
    },
    
    handleLogout() {
      this.authStore.setLogout()
      this.activeTab = 'goodsMadeInfo'
      this.changePasswordForm = { accountId: '', oldPassword: '', newPassword: '' }
      this.resetGoodsForm()
      document.cookie = 'accountId=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'
      try { this.$router.push('/') } catch (e) { window.location.href = '/' }
    },
    
    goHome() {
      try { this.$router.push('/') } catch (e) { window.location.href = '/' }
    }
  }
}
</script>

<style scoped>
/* 新色彩主题 */
:root {
  --primary-color: #409EFF;
  --primary-light: #ecf5ff;
  --primary-dark: #3375b9;
  --success-color: #67C23A;
  --warning-color: #E6A23C;
  --danger-color: #F56C6C;
  --info-color: #909399;
  --text-primary: #2c3e50;
  --text-regular: #5a6c7d;
  --text-secondary: #8796a8;
  --text-placeholder: #c0c8d1;
  --bg-gradient: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  --bg-card: #ffffff;
  --bg-sidebar: linear-gradient(180deg, #304156 0%, #1f2d3d 100%);
  --border-color: #e4e7ed;
  --border-light: #ebeef5;
  --border-lighter: #f2f6fc;
  --shadow-card: 0 4px 20px rgba(0, 0, 0, 0.08);
  --shadow-hover: 0 6px 24px rgba(0, 0, 0, 0.1);
  --shadow-header: 0 2px 12px rgba(0, 0, 0, 0.1);
}

/* 整体布局 */
.common-layout {
  height: 100vh;
  width: 100vw;
  background: var(--bg-gradient);
}

.el-container {
  height: 100%;
}

/* 顶部导航栏 */
.el-header {
  background: #ffffff;
  box-shadow: var(--shadow-header);
  display: flex;
  align-items: center;
  padding: 0 24px;
  border-bottom: 1px solid var(--border-color);
  height: 0px;
  line-height: 60px;
}

/* 侧边栏 */
.el-aside {
  background: var(--bg-sidebar);
  box-shadow: 2px 0 8px 0 rgba(29, 35, 41, 0.1);
  overflow-y: auto;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid #2d3a4b;
  color: white;
  text-align: center;
}

.sidebar-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.sidebar-menu {
  padding: 16px 0;
}

.menu-item {
  width: 100%;
  padding: 12px 24px;
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.7);
  text-align: left;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
  display: block;
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #ffffff;
}

.menu-item.active {
  background: var(--primary-color);
  color: #ffffff;
}

.sidebar-login-prompt {
  padding: 20px;
  text-align: center;
}

.back-home-tab {
  padding: 10px 20px;
  margin: 10px auto;
  border: 1px solid var(--primary-color);
  background: transparent;
  color: var(--primary-color);
  cursor: pointer;
  display: block;
  width: 100%;
  border-radius: 4px;
  font-size: 14px;
  transition: all 0.3s ease;
}

.back-home-tab:hover {
  background: var(--primary-color);
  color: white;
}

.sidebar-footer {
  margin-top: auto;
  margin-top: 300%;
  padding: 16px;
  width: 100%;
  box-sizing: border-box;
}

.logout-btn {
  width: 80%;
  margin: 0 auto;
  display: block;
  padding: 10px 20px;
  border: none;
  background: var(--danger-color);
  color: white;
  cursor: pointer;
  border-radius: 4px;
  font-size: 14px;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  background: #e4393c;
}

/* 主内容区 */
.el-main {
  padding: 24px;
  overflow-y: auto;
  background: transparent;
}

.main-content {
  min-height: 100%;
}

/* 业务卡片 */
.business-card {
  background: var(--bg-card);
  border-radius: 8px;
  box-shadow: var(--shadow-card);
  padding: 24px;
  margin-bottom: 20px;
  transition: all 0.3s ease;
  border: 1px solid var(--border-light);
}

.business-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-hover);
}

.business-card h3 {
  color: var(--text-primary);
  margin-bottom: 20px;
  font-size: 18px;
  font-weight: 600;
}

/* 表单样式 */
.password-form, .goods-form {
  max-width: 500px;
  margin: 0 auto;
}

.form-group {
  margin-bottom: 1.25rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
  color: var(--text-regular);
}

.form-group input, .form-group textarea, .form-group select {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  transition: border-color 0.3s ease;
}

.form-group input:focus, .form-group textarea:focus, .form-group select:focus {
  outline: none;
  border-color: var(--primary-color);
}

.form-group textarea {
  height: 100px;
  resize: vertical;
}

.submit-btn {
  width: 100%;
  padding: 10px;
  background: var(--success-color);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s ease;
}

.submit-btn:hover {
  background: #5daf34;
}

.form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1.5rem;
}

.cancel-btn {
  padding: 10px;
  background: var(--danger-color);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s ease;
  flex: 1;
}

.cancel-btn:hover {
  background: #e4393c;
}

.form-actions .submit-btn {
  flex: 2;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .el-container {
    grid-template-columns: 1fr;
    grid-template-rows: 60px 1fr;
    grid-template-areas: 
      "header"
      "main";
  }
  
  .el-aside {
    display: none;
  }
  
  .el-main {
    padding: 16px;
  }
  
  .business-card {
    padding: 16px;
  }
  
  .form-actions {
    flex-direction: column;
  }
}
</style>